/*
 * UpdateAccountFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import com.insigma.yanwenlong.bookkeeping.pojo.Account;
import com.insigma.yanwenlong.bookkeeping.pojo.Member;
import com.insigma.yanwenlong.bookkeeping.service.AccountService;
import com.insigma.yanwenlong.bookkeeping.service.AccountServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.MemberService;
import com.insigma.yanwenlong.bookkeeping.service.MemberServiceImpl;


/**
 *
 * @author  __USER__
 */
public class UpdateAccountFrame extends javax.swing.JFrame {
	private AccountService accountService = new AccountServiceImpl();
	private MemberService memberService = new MemberServiceImpl();
	private JTable tabAccountInfo;
	private Account account;

	/** Creates new form UpdateAccountFrame */
	public UpdateAccountFrame() {
		initComponents();
		//居中
		setLocationRelativeTo(null);
		//关闭当前窗体而不关闭父窗体
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public UpdateAccountFrame(JTable tabAccountInfo) {
		this();
		this.tabAccountInfo = tabAccountInfo;
		int id = (Integer) tabAccountInfo.getValueAt(tabAccountInfo
				.getSelectedRow(), 0);
		//显示当前选中行的账户信息
		showSelectAccount(id);
	}

	private void showSelectAccount(int id) {
		try {
			account = accountService.findAccountById(id);
			txtUpdateAccountName.setText(account.getName());
			spnUpdateMoney.setValue(account.getInitAccount());
			List<Member> members = memberService.findallMembers();
			DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
			for (Member member : members) {
				if (member.getId() == account.getMemberId()) {
					dcbm.setSelectedItem(member);
				}
				dcbm.addElement(member);
			}
			cobUpdateMember.setModel(dcbm);
			jtaUpdateNote.setText(account.getNote());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		txtUpdateAccountName = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		spnUpdateMoney = new javax.swing.JSpinner();
		jLabel3 = new javax.swing.JLabel();
		cobUpdateMember = new javax.swing.JComboBox();
		jLabel4 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jtaUpdateNote = new javax.swing.JTextArea();
		btnUpdate = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u8d26\u6237\u540d\u79f0");

		jLabel2.setText("\u521d\u59cb\u91d1\u989d");

		spnUpdateMoney.setModel(new javax.swing.SpinnerNumberModel(Double
				.valueOf(0.0d), Double.valueOf(0.0d), null, Double
				.valueOf(100.0d)));

		jLabel3.setText("\u6240\u6709\u4eba");

		cobUpdateMember.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

		jLabel4.setText("\u8bf4\u660e");

		jtaUpdateNote.setColumns(20);
		jtaUpdateNote.setRows(5);
		jScrollPane1.setViewportView(jtaUpdateNote);

		btnUpdate.setText("\u4fee\u6539");
		btnUpdate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnUpdateActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																		.addComponent(
																				jLabel2,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				jLabel1,
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE))
														.addComponent(
																jLabel4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																38,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jLabel3,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																48,
																Short.MAX_VALUE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																0, 0,
																Short.MAX_VALUE)
														.addComponent(btnUpdate)
														.addComponent(
																cobUpdateMember,
																javax.swing.GroupLayout.Alignment.TRAILING,
																0, 90,
																Short.MAX_VALUE)
														.addComponent(
																spnUpdateMoney,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																90,
																Short.MAX_VALUE)
														.addComponent(
																txtUpdateAccountName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																90,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(28, 28, 28)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																txtUpdateAccountName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel1))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																spnUpdateMoney,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel3)
														.addComponent(
																cobUpdateMember,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				10,
																				10,
																				10)
																		.addComponent(
																				jLabel4))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				56,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addGap(30, 30, 30)
										.addComponent(btnUpdate)
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击修改，将修改后的账户信息添加到数据库中
	private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
		Account account1 = new Account();
		account1.setId(account.getId());
		account1.setName(txtUpdateAccountName.getText());
		account1.setInitAccount((Double)spnUpdateMoney.getValue());
		
		double balance = account.getCurrAccount();
		double initBalance = account.getInitAccount();
		account1.setCurrAccount(account1.getInitAccount() - initBalance + balance);
		account1.setCreateTime(account.getCreateTime());
		Member member = (Member)cobUpdateMember.getSelectedItem();
		account1.setMemberId(member.getId());
		account1.setNote(jtaUpdateNote.getText());
		
		try {
			int rowAffected = accountService.updateAccount(account1);
			if(rowAffected > 0) {
				JOptionPane.showMessageDialog(this, "修改账户信息成功");
			}else {
				JOptionPane.showMessageDialog(this, "修改账户信息失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new UpdateAccountFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnUpdate;
	private javax.swing.JComboBox cobUpdateMember;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jtaUpdateNote;
	private javax.swing.JSpinner spnUpdateMoney;
	private javax.swing.JTextField txtUpdateAccountName;
	// End of variables declaration//GEN-END:variables

}